package me.lc.hammerheads.sprite;

import me.lc.hammerheads.R;
import me.lc.hammerheads.core.GameManager;
import me.lc.hammerheads.core.GameSystem;
import me.lc.hammerheads.core.StageManager;
import me.lc.hammerheads.scene.MainScene;
import me.lc.hammerheads.util.ADHelper;
import me.lc.hammerheads.util.AudioHelper;
import me.lc.hammerheads.util.GameUtil;
import android.app.Activity;

import com.wiyun.engine.nodes.Button;
import com.wiyun.engine.nodes.Label;
import com.wiyun.engine.nodes.Node;
import com.wiyun.engine.nodes.Sprite;
import com.wiyun.engine.types.WYColor3B;

public class PauseShopBlock extends Node {

	Label goldLabel;
	Button btnDoubleHammer;
	Button btnBomb;
	Button btnHeart;

	Button btnResume;
	Button btnMenu;
	Sprite bg;

	static int goldFontSize = 25;

	public PauseShopBlock() {
		bg = Sprite.make(GameSystem.pauseBGTex);
		addChild(bg);

		goldLabel = Label.make("", goldFontSize);
		float goldX = 0;
		float goldY = GameSystem.pauseBGTex.getHeight() * 0.2915f;
		goldLabel.setPosition(goldX, goldY);
		goldLabel.setColor(WYColor3B.make(210, 150, 0));
		addChild(goldLabel);

		btnDoubleHammer = Button.make(R.drawable.button_buy,
				R.drawable.button_buy, this, "onBuyHammerClicked");
		float btnX = GameSystem.pauseBGTex.getWidth() * 0.2652f;
		float btnFinderY = GameSystem.pauseBGTex.getHeight() * 0.1438f;
		btnDoubleHammer.setPosition(btnX, btnFinderY);
		btnDoubleHammer.setClickScale(1.1f);
		addChild(btnDoubleHammer);

		btnBomb = Button.make(R.drawable.button_buy, R.drawable.button_buy,
				this, "onBuyBombClicked");
		float btnArrangeY = GameSystem.pauseBGTex.getHeight() * 0.0183f;
		btnBomb.setPosition(btnX, btnArrangeY);
		btnBomb.setClickScale(1.1f);
		addChild(btnBomb);

		btnHeart = Button.make(R.drawable.button_buy, R.drawable.button_buy,
				this, "onBuyHeartClicked");
		float btnTimeY = GameSystem.pauseBGTex.getHeight() * (0.3988f - 0.5f);
		btnHeart.setPosition(btnX, btnTimeY);
		btnHeart.setClickScale(1.1f);
		addChild(btnHeart);

		btnResume = Button.make(R.drawable.button_continue,
				R.drawable.button_continue_press, this, "onResumeClicked");
		float systemBtnY = (0.1f - 0.5f) * GameSystem.pauseBGTex.getHeight();
		float resumeX = (0.2637f - 0.5f) * GameSystem.pauseBGTex.getWidth();
		btnResume.setPosition(resumeX, systemBtnY);
		addChild(btnResume);

		btnMenu = Button.make(R.drawable.button_returnmenu_pause,
				R.drawable.button_returnmenu_pause_press, this,
				"onMainmenuClicked");
		float menuX = 0.2362f * GameSystem.pauseBGTex.getWidth();
		btnMenu.setPosition(menuX, systemBtnY);
		addChild(btnMenu);

	}

	public void updateGold() {
		goldLabel.setText(StageManager.instance.goldCount + "");
	}

	public void show() {
		goldLabel.setText(StageManager.instance.goldCount + "");
	}

	public void onBuyHammerClicked() {
		int cost = 300;
		if (StageManager.instance.goldCount >= cost) {
			StageManager.instance.goldCount -= cost;
			StageManager.instance.saveGoldCount();
			updateGold();
			StageManager.instance.doubleHammerCount = StageManager.instance.doubleHammerCount + 1;
			saveAssertInThread();
			GameManager.instance.refreshToolCount();
			AudioHelper.buyTool();
		}
	}

	private void saveAssertInThread() {
		Thread t = new Thread() {
			@Override
			public void run() {
				StageManager.instance.saveToolsCount();
			}
		};
		t.start();
	}

	public void onBuyBombClicked() {
		int cost = 300;
		if (StageManager.instance.goldCount >= cost) {
			StageManager.instance.goldCount -= cost;
			StageManager.instance.saveGoldCount();
			updateGold();
			StageManager.instance.bombCount++;
			saveAssertInThread();
			GameManager.instance.refreshToolCount();
			AudioHelper.buyTool();

		}
	}

	public void onBuyHeartClicked() {
		int cost = 600;
		if (StageManager.instance.goldCount >= cost) {
			StageManager.instance.goldCount -= cost;
			StageManager.instance.saveGoldCount();
			updateGold();
			StageManager.instance.addHeartCount++;
			saveAssertInThread();
			GameManager.instance.refreshToolCount();
			AudioHelper.buyTool();
		}
	}

	public void onResumeClicked() {
		((Activity) GameSystem.AndroidContext).runOnUiThread(new Runnable() {
			public void run() {
				AudioHelper.buttonClick();
				GameManager.instance.resumeGame();
				PauseShopBlock.this.setScale(0.5f);
			}
		});
	}

	public void onMainmenuClicked() {
		((Activity) GameSystem.AndroidContext).runOnUiThread(new Runnable() {
			public void run() {
				AudioHelper.buttonClick();
				MainScene.make().showMainMenu();
				GameManager.instance.quitGame();
				GameUtil.switchSceneFlipX(MainScene.make());
				ADHelper.hideAd();
				PauseShopBlock.this.setScale(0.5f);
				if (StageManager.instance.enableAudio == 1) {
					AudioHelper.playMusicMenu();
				}
			}
		});
	}
}
